﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace ProjectCoop_Interface
{
    public partial class Form1 : Form
    {
        public string appName = "Project Coop";
        private bool startUp = true;
        public Form1()
        {
            InitializeComponent();
            ResetWindow();
        }

        private void ResetWindow()
        {
            this.Text = appName;
            gamePath.Text = "";
            isClient.Checked = false;
            isServer.Checked = false;
            IsTypeEnabler();
            this.Update();
            this.Validate();
        }

        private void run_application_Click(object sender, EventArgs e)
        {
            ProcessStartInfo start = new ProcessStartInfo();

            start.Arguments = gamePath.Text;
            start.FileName = "ProjectCoop.exe";
            start.WindowStyle = ProcessWindowStyle.Normal;
            start.CreateNoWindow = true;

            try
            {
                using (Process proc = Process.Start(start))
                {
                    this.Enabled = false;
                    this.Text = appName + " (Running...)";

                    proc.WaitForExit();

                    // Retrieve the app's exit code
                    int exitCode = proc.ExitCode;

                    if (exitCode == 0)
                    {
                        if (MessageBox.Show("Would you like to connect to another game?", "Continue?", 
                            MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 
                            MessageBoxOptions.DefaultDesktopOnly) == DialogResult.No)
                        {
                            this.Close();
                            return;
                        }

                        startUp = true;
                    }

                    this.BringToFront();
                    this.Activate();

                    ResetWindow();
                    this.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                // TODO: Add error code
                ex.ToString();
            }
        }

        private void IsTypeEnabler()
        {
            run_application.Enabled = false;

            // handle server stuff
            bool serverEnab = isServer.Checked;
            selectGameButton.Enabled = serverEnab;
            portBox1.Enabled = serverEnab;
            portLabel1.Enabled = serverEnab;
            gamePath.Enabled = serverEnab;

            if (serverEnab == true)
                run_application.Enabled = true;

            // handle client stuff
            bool clientEnab = isClient.Checked;
            portBox2.Enabled = clientEnab;
            portLabel2.Enabled = clientEnab;
            ipAddressControl.Enabled = clientEnab;
            ipLabel.Enabled = clientEnab;

            if (clientEnab == true)
                run_application.Enabled = true;
        }

        private void isServer_MouseClick(object sender, MouseEventArgs e)
        {
            isClient.Checked = false;
            IsTypeEnabler();
        }

        private void isClient_MouseClick(object sender, MouseEventArgs e)
        {
            isServer.Checked = false;
            IsTypeEnabler();
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            if (startUp)
            {
                this.ResetWindow();
                startUp = false;
            }
        }

        private void portBox1_TextChanged(object sender, EventArgs e)
        {
            int retVal = 0;
            //if (int.TryParse(portBox1.Text, out retVal) == false)
            //    portBox1.Text = "";
            for (int i = 0; i < portBox1.Text.Count(); ++i)
            {
                if (int.TryParse(portBox1.Text, out retVal) == false)
                {
                    portBox1.Text = portBox1.Text.Remove(i);
                    --i;
                }
            }
        }

        private void portBox2_TextChanged(object sender, EventArgs e)
        {
            int retVal = 0;
            //if (int.TryParse(portBox2.Text, out retVal) == false)
            //    portBox2.Text = "";
            for (int i = 0; i < portBox2.Text.Count(); ++i)
            {
                if (int.TryParse(portBox2.Text, out retVal) == false)
                {
                    portBox2.Text = portBox2.Text.Remove(i);
                    --i;
                }
            }
        }

        private void selectGameButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Filter = "*.exe|*.*";

            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                string filePath = dlg.FileName;

                if (!File.Exists(filePath))
                {
                    MessageBox.Show("Not a valid file");
                    return;
                }

                gamePath.Text = filePath;
            }
        }

    }
}
